package com.service.impl;

import com.controller.WebSocketController;
import com.event.NotificationEvent;
import com.mapper.FollowsMapper;
import com.pojo.Follows;
import com.pojo.Notifications;
import com.pojo.Result;
import com.service.FollowsService;
import com.service.NotificationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class FollowsServiceImpl implements FollowsService {

    @Autowired
    private FollowsMapper followsMapper;

    @Autowired
    private NotificationsService notificationsService;
    @Autowired
    private WebSocketController webSocketController;

    @Autowired
    private ApplicationEventPublisher publisher;

    @Override
    public Result<Void> followUser(Integer followerId, Integer followedId) {
        try {
            if (followsMapper.exists(followerId, followedId) > 0) {
                return Result.error("您已经关注了该用户");
            }

            Follows follow = new Follows();
            follow.setFollowerId(followerId);
            follow.setFollowedId(followedId);
            follow.setCreatedAt(LocalDateTime.now());

            int result = followsMapper.insert(follow);
            if (result <= 0) {
                return Result.error("关注用户失败");
            }

            Notifications notification = new Notifications();
            notification.setUserId(followedId);
            notification.setType("FOLLOW");
            notification.setSourceId(followerId);
            notification.setTargetId(followerId);
            notification.setRead(false);
            notification.setCreatedAt(LocalDateTime.now());

            // 保存通知到数据库
            notificationsService.createNotification(notification);

            // 发布事件（解耦推送）
            publisher.publishEvent(new NotificationEvent(notification));

            return Result.success();
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> unfollowUser(Integer followerId, Integer followedId) {
        try {
            int result = followsMapper.delete(followerId, followedId);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("取消关注失败，可能未关注");
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> unfollowAllByFollower(Integer followerId) {
        try {
            int result = followsMapper.deleteByFollowerId(followerId);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.success(); // 没有关注也算成功
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Void> unfollowAllByFollowed(Integer followedId) {
        try {
            int result = followsMapper.deleteByFollowedId(followedId);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.success(); // 没有被关注也算成功
            }
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Boolean> isFollowing(Integer followerId, Integer followedId) {
        try {
            int exists = followsMapper.exists(followerId, followedId);
            return Result.success(exists > 0);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Integer> getFollowingCount(Integer followerId) {
        try {
            int count = followsMapper.countFollowing(followerId);
            return Result.success(count);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<Integer> getFollowersCount(Integer followedId) {
        try {
            int count = followsMapper.countFollowers(followedId);
            return Result.success(count);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<List<Follows>> getFollowingList(Integer followerId) {
        try {
            List<Follows> following = followsMapper.selectByFollowerId(followerId);
            return Result.success(following);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }

    @Override
    public Result<List<Follows>> getFollowersList(Integer followedId) {
        try {
            List<Follows> followers = followsMapper.selectByFollowedId(followedId);
            return Result.success(followers);
        } catch (Exception e) {
            return Result.error(500, "服务器内部错误: " + e.getMessage());
        }
    }
}